package com.yzt.dynamicmodule.dialog;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.DialogInterface;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yzt.dynamicmodule.R;
import com.yzt.zhuju.dialog.CenterBaseDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.game.GamePunishPriceBean;

/**
 * 作者：bobwang
 * 创建时间：2024-01-07 19:27:06
 * 描述：打开盲盒
 */
public class GamePunishOpenDialog extends CenterBaseDialog {

    private ImageView imgContainer;

    private View viewOpen;

    private TextView tvPunish;

    private String activityId;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_game_punish_open;
    }

    @Override
    public void bindView(View v) {
        initView();
    }

    private void initView() {
        activityId = getArguments().getString("extra_game_id");
        imgContainer = findViewById(R.id.img_container);
        ImageView imClose = findViewById(R.id.img_close);
        tvPunish = findViewById(R.id.tv_punish);
        imClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });
        viewOpen = findViewById(R.id.view_open);
        viewOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewOpen.setVisibility(View.GONE);
                BizLayer.getInstance().getMineModule().punishShark(activityId, new AresSubscriber<GamePunishPriceBean>() {
                    @Override
                    protected void onSuccess(GamePunishPriceBean bean) {
                        imgContainer.setImageResource(R.mipmap.icon_game_punish_open_loading);
                        tvPunish.setText(bean.getDsc());
                        anim2();
                    }
                });
            }
        });
        anim1();
    }

    /**
     * 第一步动画，初始化的时候1.2 - > 1.0
     */
    private void anim1() {
        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_punish_1);
        imgContainer.startAnimation(animation);
    }

    /**
     * 摇晃动画
     */
    private void anim2() {
        shake(imgContainer);
    }

    public void shake(View view) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.08f, 1f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.08f, 1f);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", 0f, 15f, 0f, -15f, 0f, 12f, 0f, -12f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, 9f, 0f, -9f, 0f, 6f, 0f, -6f, 0f, 3f, 0f, -3f, 0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animatorX, animatorY, rotation);
        animatorSet.setDuration(1000);
        animatorSet.setInterpolator(new LinearInterpolator());
        animatorSet.setStartDelay(1200);

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                imgContainer.setImageResource(R.mipmap.icon_game_punish_result_bg_big);
                tvPunish.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }
        });

        animatorSet.start();
    }

    private OnDismissListener onDismissListener;

    public void setOnDismissListener(OnDismissListener onBaseDialogDismiss) {
        if (onBaseDialogDismiss != null) {
            this.onDismissListener = onBaseDialogDismiss;
        }
    }

    public interface OnDismissListener {
        void onDismiss();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        if (onDismissListener != null) {
            onDismissListener.onDismiss();
        }
        super.onDismiss(dialog);
    }
}
